// Author: Mohamed Abdel Hakim

$(document).ready(function()
{
    $('#nav a').click(function(event) { // function to handle click events of menu anchors
        hideLoading();
        event.preventDefault();
        var page = $(this).attr('href'); // getting the href of the anchor
        $('#collabsoft').fadeOut('slow',function() {
            $('#collabsoft').attr("src",page); // changing the src of the iframe with call backs fadeout/fadein to give the fading effect
        });
        return false;
    });
    $('#footer a').click(function(event) {
        hideLoading(); //function to handle click events of footer anchors
        event.preventDefault();
        var page = $(this).attr('href');             // same scenario as before
        $('#collabsoft').fadeOut('slow',function() {
            $('#collabsoft').attr("src",page);
        });
        return false;
    });
    $("#collabsoft").load(function() // form to handle anchors click & form submits inside the iframe
    {
        hideLoading();
        $(this).fadeIn('slow'); // always fadeIn on load
        $("#collabsoft").contents().find("a").bind("click",function (event) //binding click event for all anchors
        {
            var page = $(this).attr('href');
            if(page=="#") // checking if the link is empty or invalid link
                return;
            else
                event.preventDefault();

            if($(this).attr("target")=="_blank") // if target _blank, open page in new tab
            {
                window.open(page,"_blank");
            }
            else
            {
                hideLoading();
                $("#collabsoft").fadeOut("slow", function() // fadeout then change src, on load the iframe will fadeIn again.
                {
                    $('#collabsoft').attr("src",page);
                });
            }
            return false;
        });
        $('#collabsoft').contents().find('form input[type=submit]').bind('click', function(event) {
            hideLoading(); // bining click event for all submit buttons
            element=$(this).parent("form"); // getting the parent form the submit button
            var page = $(element).attr("action"); // getting action of the form
            var type = $(element).attr("enctype");
            if (type == "multipart/form-data") {
                $("#collabsoft").fadeOut('slow');// allow default to load new action
                $(element).submit();
            }
            else if (page == "") {
                $("#collabsoft").fadeOut('slow', function(){ //if action is null submit form in the same page & prevent default
                    event.preventDefault();
                    $(element).submit();
                });
            }else {
                $("#collabsoft").fadeOut('slow');// allow default to load new action
                $(element).submit();

            }
        });
    });
});
